package nl.hr.cmi.opleiding;

import java.util.ArrayList;
import java.util.List;

import nl.hr.cmi.opleiding.controller.ShowcaseManager;
import nl.hr.cmi.opleiding.database.DBShowcaseItem;
import nl.hr.cmi.opleiding.database.DBShowcaseItemDatabaseHelper;
import nl.hr.cmi.opleiding.models.ShowcaseAppItem;
import nl.hr.cmi.opleiding.utils.ShowcaseView;
import android.content.Intent;
import android.content.res.Resources;
import android.net.Uri;
import android.opengl.Visibility;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.j256.ormlite.android.apptools.OrmLiteBaseActivity;
import com.j256.ormlite.dao.RuntimeExceptionDao;

public class ShowcaseZoom extends OrmLiteBaseActivity<DBShowcaseItemDatabaseHelper> {

	ShowcaseManager sm;
	TextView tv_title;
	TextView tv_subtitle;
	TextView tv_tekst;
	ImageView iv_icon;
	ImageView iv_youtube;
	LinearLayout ll_market;

	private int case_id;

	DBShowcaseItem item;

	Resources r;

	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.showcase_zoom);

		tv_title = (TextView) findViewById(R.id.showcase_zoom_title);
		tv_subtitle = (TextView) findViewById(R.id.showcase_zoom_subtitle);
		tv_tekst = (TextView) findViewById(R.id.showcase_zoom_text);
		iv_icon = (ImageView) findViewById(R.id.showcase_zoom_icon);
		iv_youtube = (ImageView) findViewById(R.id.showcase_zoom_youtube);
		ll_market = (LinearLayout) findViewById(R.id.showcase_zoom_market);

		r = getResources();

		// retrieve database data
		Bundle extras = getIntent().getExtras();
		case_id = Integer.parseInt(extras.getString(ShowcaseView.CASE_ID));
		Log.d(ShowcaseZoom.class.toString(), extras.getString(ShowcaseView.CASE_ID));

		RuntimeExceptionDao<DBShowcaseItem, Integer> showcaseDao = getHelper().getDBShowcaseItemDao();
		// query for all of the data objects in the database
		item = (DBShowcaseItem) showcaseDao.queryForId(case_id);

		tv_title.setText(item.getTitle());
		tv_subtitle.setText(item.getSubtitle());
		tv_tekst.setText(item.getText());

		int drawableId = r.getIdentifier(item.getAfbeelding(), "drawable", "nl.hr.cmi.opleiding");
		iv_icon.setImageResource(drawableId);// TODO make a default image when
												// all else fails

		if (item.getMarket() != null) {
			if (item.getPlatform() == DBShowcaseItem.PLATFORM_ANDROID) {
				ImageView i = new ImageView(getBaseContext());
				i.setImageResource(R.drawable.androidmarket);
				ll_market.addView(i, 0);
				ll_market.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(item.getMarket())));
					}
				});
			}
		} else {
			ll_market.setVisibility(View.INVISIBLE);
		}

		if (item.getMovie() != null) {
			iv_youtube.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(item.getMovie())));
				}
			});
		} else {
			iv_youtube.setVisibility(View.INVISIBLE);
		}

	}
}
